<template>
  <div id="UserAdd">
    <el-card style="width: 98%; height: 40vh" shadow="always">
      <div
        class="category-top"
        style="
          display: flex;
          justify-content: space-between;
          align-items: center;
          line-height: 50px;
          border-bottom: 2px solid rgb(244, 245, 250);
        "
      >
        <h3>添加账号</h3>
      </div>

      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="ruleForm.account" style="background-color: #fff" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            style="background-color: #fff"
          />
        </el-form-item>
        <el-form-item label="用户组" prop="userGroup">
          <el-select
            v-model="ruleForm.userGroup"
            placeholder="请选择用户组"
            style="background-color: #fff"
          >
            <el-option
              v-for="item in userGroups"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button :plain="true" type="primary" @click="submitForm(ruleFormRef)">添加</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { fetchUserAdd } from '../api/users/index'

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  account: '',
  password: '',
  userGroup: '',
})

const userGroups = reactive([
  { value: '超级管理员', label: '超级管理员' },
  { value: '普通管理员', label: '普通管理员' },
  // 可以在这里添加更多的用户组选项
])

const rules = reactive<FormRules<typeof ruleForm>>({
  account: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === '') {
          callback(new Error('账号不能为空'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  password: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (value === '') {
          callback(new Error('请输入正确的密码'))
        } else {
          callback()
        }
      },
      trigger: 'blur',
    },
  ],
  userGroup: [
    {
      validator: (rule: any, value: any, callback: any) => {
        if (!value) {
          callback(new Error('请选择正确的用户组'))
        } else {
          callback()
        }
      },
      trigger: 'change',
    },
  ],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      console.log(ruleForm)
      const res = await fetchUserAdd(ruleForm)
      if (res.code === 0) {
        ElMessage({
          message: '添加成功',
          type: 'success',
        })
      }else{
        ElMessage({
          message: '添加失败',
          type: 'error',
        })
      }
      console.log('submit!')
    } else {
      console.log('error submit!')
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.el-input__inner {
  background-color: #fff;
}
/* 取消input死样式 */
:deep(.el-input__inner) {
  -webkit-text-fill-color: #857f7f;
  caret-color: #eee;
  box-shadow: inset 0 0 0 1100px #fff !important;
}
:deep(input::-webkit-input-placeholder) {
  -webkit-text-fill-color: #5f757f !important;
}
</style>
